#include "AX_Sched_Params.h"
 AX_Sched_Priority
AX_Sched_Params::priority (void) const
{
	return this->priority_;
}
 AX_Sched_Priority
	 AX_Sched_Params::policy (void) const
 {
	 AX_hthread_t temp;
	 AX_OS::thr_self(temp);
#ifdef _WIN32
	  AX_OS::thr_getprio(temp,(int)priority_,(int)policy_);
#else
	  AX_OS::thr_getprio((AX_hthread_t&)temp,(int&)priority_,(int &)policy_);
#endif
	 return policy_;
 }
 void AX_Sched_Params::policy (const AX_Sched_Params::Policy policy)
 {
	 AX_hthread_t temp;
	 AX_OS::thr_self(temp);
	 if (AX_OS::thr_setprio(temp,policy,priority_) != -1)
	 {
		 //printf("AX_OS::thr_setprio(temp,policy,priority_) != -1\n");
		 policy_ = policy;
	 }	 
 }

int
AX_Sched_Params::priority_min (const Policy policy,
								const int scope)
{
//#if defined (ACE_HAS_PRIOCNTL) && defined (ACE_HAS_STHREADS)
//	ACE_UNUSED_ARG (scope);
//
//	// Assume that ACE_SCHED_OTHER indicates TS class, and that other
//	// policies indicate RT class.
//
//	// Call ACE_OS::priority_control only for processes (lightweight
//	// or otherwise). Calling ACE_OS::priority_control for thread
//	// priorities gives incorrect results.
//	if (scope == ACE_SCOPE_PROCESS || scope == ACE_SCOPE_LWP)
//	{
//		if (policy == ACE_SCHED_OTHER)
//		{
//			// Get the priority class ID and attributes.
//			pcinfo_t pcinfo;
//			// The following is just to avoid Purify warnings about unitialized
//			// memory reads.
//			memset (&pcinfo, 0, sizeof pcinfo);
//			strcpy (pcinfo.pc_clname, "TS");
//
//			if (ACE_OS::priority_control (P_ALL /* ignored */,
//				P_MYID /* ignored */,
//				PC_GETCID,
//				(char *) &pcinfo) == -1)
//				// Just hope that priority range wasn't configured from -1
//				// .. 1
//				return -1;
//
//			// OK, now we've got the class ID in pcinfo.pc_cid.  In
//			// addition, the maximum configured time-share priority is in
//			// ((tsinfo_t *) pcinfo.pc_clinfo)->ts_maxupri.  The minimum
//			// priority is just the negative of that.
//
//			return -((tsinfo_t *) pcinfo.pc_clinfo)->ts_maxupri;
//		}
//		else
//			return 0;
//	}
//	else
//	{
//		// Here we handle the case for ACE_SCOPE_THREAD. Calling
//		// ACE_OS::priority_control for thread scope gives incorrect
//		// results.
//		switch (policy)
//		{
//		case ACE_SCHED_FIFO:
//			return ACE_THR_PRI_FIFO_MIN;
//		case ACE_SCHED_RR:
//			return ACE_THR_PRI_RR_MIN;
//		case ACE_SCHED_OTHER:
//		default:
//			return ACE_THR_PRI_OTHER_MIN;
//		}
//	}
//#elif defined(ACE_HAS_PTHREADS) && \  
//	(!defined(ACE_LACKS_SETSCHED) || defined (ACE_TANDEM_T1248_PTHREADS) || \  
//	defined (ACE_HAS_PTHREAD_SCHEDPARAM))
#ifndef _WIN32
		switch (policy)
		{
		case SCHED_FIFO:
			return sched_get_priority_min(SCHED_FIFO);
		case SCHED_RR:
			return sched_get_priority_min(SCHED_RR);
		case SCHED_OTHER:
		default:
			return sched_get_priority_min(SCHED_OTHER);
		}

#else
	return THREAD_PRIORITY_IDLE;
#endif 
}

int
AX_Sched_Params::priority_max (const Policy policy,
								const int scope)
{
//#if defined (ACE_HAS_PRIOCNTL) && defined (ACE_HAS_STHREADS)
//
//	// Call ACE_OS::priority_control only for processes (lightweight
//	// or otherwise). Calling ACE_OS::priority_control for thread
//	// priorities gives incorrect results.
//	if (scope == ACE_SCOPE_PROCESS || scope == ACE_SCOPE_LWP)
//	{
//		// Assume that ACE_SCHED_OTHER indicates TS class, and that other
//		// policies indicate RT class.
//
//		// Get the priority class ID and attributes.
//		pcinfo_t pcinfo;
//		// The following is just to avoid Purify warnings about unitialized
//		// memory reads.
//		memset (&pcinfo, 0, sizeof pcinfo);
//		strcpy (pcinfo.pc_clname,
//			policy == ACE_SCHED_OTHER  ?  "TS"  :  "RT");
//
//		if (ACE_OS::priority_control (P_ALL /* ignored */,
//			P_MYID /* ignored */,
//			PC_GETCID,
//			(char *) &pcinfo) == -1)
//			return -1;
//
//		// OK, now we've got the class ID in pcinfo.pc_cid.  In addition,
//		// the maximum configured real-time priority is in ((rtinfo_t *)
//		// pcinfo.pc_clinfo)->rt_maxpri, or similarly for the TS class.
//
//		return policy == ACE_SCHED_OTHER
//			? ((tsinfo_t *) pcinfo.pc_clinfo)->ts_maxupri
//			: ((rtinfo_t *) pcinfo.pc_clinfo)->rt_maxpri;
//	}
//	else
//	{
//		// Here we handle the case for ACE_SCOPE_THREAD. Calling
//		// ACE_OS::priority_control for thread scope gives incorrect
//		// results.
//		switch (policy)
//		{
//		case ACE_SCHED_FIFO:
//			return ACE_THR_PRI_FIFO_MAX;
//		case ACE_SCHED_RR:
//			return ACE_THR_PRI_RR_MAX;
//		case ACE_SCHED_OTHER:
//		default:
//			return ACE_THR_PRI_OTHER_MAX;
//		}
//	}
#ifndef _WIN32 
	//switch (scope)
	//{
	//case ACE_SCOPE_THREAD:
		switch (policy)
		{
		case SCHED_FIFO:
			return sched_get_priority_max(SCHED_FIFO);
		case SCHED_RR:
			return sched_get_priority_max(SCHED_RR);
		case SCHED_OTHER:
		default:
			return sched_get_priority_max(SCHED_OTHER);
		}
	//}

#else
	return THREAD_PRIORITY_TIME_CRITICAL;
#endif 
}

int
AX_Sched_Params::next_priority (const Policy policy,
								 const int priority,
								 const int scope)
{
#if defined (_WIN32)

	switch (priority)
	{
	case THREAD_PRIORITY_IDLE:
		return THREAD_PRIORITY_LOWEST;
	case THREAD_PRIORITY_LOWEST:
		return THREAD_PRIORITY_BELOW_NORMAL;
	case THREAD_PRIORITY_BELOW_NORMAL:
		return THREAD_PRIORITY_NORMAL;
	case THREAD_PRIORITY_NORMAL:
		return THREAD_PRIORITY_ABOVE_NORMAL;
	case THREAD_PRIORITY_ABOVE_NORMAL:
		return THREAD_PRIORITY_HIGHEST;
	case THREAD_PRIORITY_HIGHEST:
		return THREAD_PRIORITY_TIME_CRITICAL;
	case THREAD_PRIORITY_TIME_CRITICAL:
		return THREAD_PRIORITY_TIME_CRITICAL;
	default:
		return priority;  // unknown priority:  should never get here
	}
//#elif defined(ACE_HAS_THREADS) && \
//	(!defined(ACE_LACKS_SETSCHED) || defined (ACE_TANDEM_T1248_PTHREADS) || \
//	defined (ACE_HAS_PTHREAD_SCHEDPARAM))
#else
	// including STHREADS, and PTHREADS
	const int max = priority_max (policy, scope);
	return priority < max  ?  priority + 1  :  max;
#endif 
}

int
AX_Sched_Params::previous_priority (const Policy policy,
									 const int priority,
									 const int scope)
{
#if defined (_WIN32)
	switch (priority)
	{
	case THREAD_PRIORITY_IDLE:
		return THREAD_PRIORITY_IDLE;
	case THREAD_PRIORITY_LOWEST:
		return THREAD_PRIORITY_IDLE;
	case THREAD_PRIORITY_BELOW_NORMAL:
		return THREAD_PRIORITY_LOWEST;
	case THREAD_PRIORITY_NORMAL:
		return THREAD_PRIORITY_BELOW_NORMAL;
	case THREAD_PRIORITY_ABOVE_NORMAL:
		return THREAD_PRIORITY_NORMAL;
	case THREAD_PRIORITY_HIGHEST:
		return THREAD_PRIORITY_ABOVE_NORMAL;
	case THREAD_PRIORITY_TIME_CRITICAL:
		return THREAD_PRIORITY_HIGHEST;
	default:
		return priority;  // unknown priority:  should never get here
	}
//#elif defined(ACE_HAS_THREADS) && \  
//	(!defined(ACE_LACKS_SETSCHED) || defined (ACE_TANDEM_T1248_PTHREADS) || \  
//	defined (ACE_HAS_PTHREAD_SCHEDPARAM))
	// including STHREADS and PTHREADS
#else
	const int min = priority_min (policy, scope);

	return priority > min ? priority - 1 : min;
//#else
//	return -1;
#endif /* ACE_HAS_THREADS */
		//return 1;
}


